package com.capricornus.demo.service.user;

import com.capricornus.demo.common.ApplicationException;
import com.capricornus.demo.common.util.PageResult;
import com.capricornus.demo.common.util.PageUtils;
import com.capricornus.demo.dao.StudentMapper;
import com.capricornus.demo.model.Student;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("StudentService")
public class StudentServiceImpl implements StudentService{

    @Autowired
    private StudentMapper studentMapper;
    @Override
    public List<Student> selectList(String studentId, String studentName) {

        return studentMapper.selectList(studentId,studentName);
    }

    @Override
    public PageResult findPage(Student student) {
        return PageUtils.getPageResult(student,getPageInfo(student));
    }

    /**
     * 调用分页插件完成分页
     * @param student
     * @return
     */
    private PageInfo<Student> getPageInfo(Student student){
        int pageNum=student.getPageNum();
        int pageSize=student.getPageSize();
        PageHelper.startPage(pageNum,pageSize);
        List<Student>studentList=studentMapper.selectPage(student.getStudentId(),student.getStudentName());
        return new PageInfo<Student>(studentList);
    }

    @Override
    public Student selectByPrimaryKey(String studentId) {

        return studentMapper.selectByPrimaryKey(studentId);
    }

    @Override
    public int updateStudentValId(String studentId) {

        int updateCount=studentMapper.updateStudentValId(studentId);
        if (updateCount<1){
            throw new ApplicationException("逻辑删除失败");
        }
        return updateCount;
    }

    @Override
    public int deleteByStudentId(String studentId) {
        int deleteCount1=studentMapper.deleteByStudentId(studentId);
        if (deleteCount1<1){
            throw new ApplicationException("物理删除失败");
        }
        return deleteCount1;
    }

    @Override
    public int insert(Student student) {
        int insertCount=studentMapper.insert(student);
        if (insertCount<=0){
            throw new ApplicationException("新增失败");
        }
        return insertCount;
    }

    @Override
    public int update(Student student) {
        int updateCount=studentMapper.update(student);
        if (updateCount<=0){
            throw new ApplicationException("修改失败");
        }
        return updateCount;
    }
}
